$(function () {
  // 调用函数
  let layer = layui.layer

  initArtCateList()
  // 封装
  function initArtCateList () {
    axios({
      method: 'get',
      url: '/my/article/cates'
    }).then(res => {
      // console.log(res)
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      // 成功后直接渲染
      let arr = []
      res.data.data.forEach(function (ele) {
        if (ele.Id < 0) return
        arr.push(`
             <tr>
                 <td>${ele.Id}</td>
                 <td>${ele.name}</td>
                 <td>${ele.alias}</td>
                 <td>
                    <button data-id="${ele.Id}" class="btn-edit layui-btn layui-btn-xs">修改</button>
                    <button data-id="${ele.Id}" class="del-edit layui-btn layui-btn-xs layui-btn-danger">删除</button>
                 </td>
             </tr>
         `)
      })

      $('tbody')
        .empty()
        .html(arr.join(''))
    })
  }

  // 点击按钮,弹出添加类别对话框
  // 定义一个全局变量,接收open(的索引值,将来用于关闭对话框
  let indexAdd = 0
  $('#addBtn').on('click', function () {
    layer.open({
      type: 1,
      title: '添加文章类别',
      area: ['500px', '260px'],
      content: `
            <form id="addForm" class="layui-form" action="">
                <div class="layui-form-item">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                            class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类别名</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                            class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
          `
    })
  })

  // 添加文章分类
  $('body').on('submit', '#addForm', function (e) {
    e.preventDefault()
    axios({
      method: 'post',
      url: '/my/article/addcates',
      data: $(this).serialize()
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      // 成功 : 提示,初始化文章分类
      layer.msg('添加文章类别成功')
      initArtCateList()
      // 关闭对话框
      layer.close(indexAdd)
    })
  })

  // 修改分类弹出层
  let indexEdit = 0
  $('tbody').on('click', '.btn-edit', function () {
    indexEdit = layer.open({
      type: 1,
      title: '修改文章类别',
      area: ['500px', '260px'],
      content: `
            <form id="editForm" class="layui-form" lay-filter="editForm">
                <input type="hidden" name="Id">
                <div class="layui-form-item">
                    <label class="layui-form-label">分类名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                            class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">分类别名</label>
                    <div class="layui-input-block">
                        <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                            class="layui-input" />
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="formDemo">确认修改</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
          `
    })
    // 点击编辑按钮做两件事
    // 获取数据和渲染数据
    let id = $(this).attr('data-id')
    // 发送axios
    axios({
      method: 'get',
      //  /:id 动态参数(路由参数)
      // 特点 :/ 不能省略 :一定要去掉
      url: '/my/article/cates/' + id
    }).then(res => {
      // console.log(res)
      if(res.data.status !== 0) {
          return layer.msg(res.data.message)
      }
      // 成功后渲染
      layui.form.val('editForm',res.data.data)
    })
  })

  // 修改文字分类
  $('body').on('submit', '#editForm', function (e) {
    e.preventDefault()
    //修改分类
    axios({
      method: 'post',
      url: '/my/article/updatecate',
      data: $(this).serialize()
    }).then(res => {
      if (res.data.status !== 0) {
        return layer.msg(res.data.message)
      }
      // 成功 : 提示,初始化文章分类
      layer.msg('修改文章类别成功')
      initArtCateList()
      // 关闭对话框
      layer.close(indexAdd)
    })
  })

  // 删除
  $('tbody').on('click','.del-edit',function () {
      // 获取自定义属性值
      let id = $(this).attr('data-id')
      // 弹框
      layer.confirm('确认删除吗?', {icon: 3, title:'提示'}, function(index){
        //发送ajax
        axios({
          url: '/my/article/deletecate/' + id
        }).then(res => {
          if (res.data.status !== 0) {
            return layer.msg(res.data.message)
          }
          // 成功: 提示,初始化文章分类列表
          layer.msg('删除文章类别成功')
          initArtCateList()
        })
        // 关闭询问框
        layer.close(index);
      });
  })

})
